$(function ($) {
    window.onload=function () {
        $("#butt").click();
    }
});

layui.use(['layer', 'form'], function() {

    var layer = layui.layer
        ,form = layui.form;

    $(function() {

        $("#butt").click(function() {
            layer.open({
                type: 1,
                title: "注册页面",
                area: ['510px', '650px'],
                offset: '20px',
                content: $("#gb"),
                cancel: function() {
                    // 你点击右上角 X 取消后要做什么
                    window.location.href="/toLogin";
                },
                success: function() {
                    form.on('submit(formDemo)', function(data) {
                        var uAccount = $("#account").val();
                        var uPassword = $("#pass1").val();
                        var uName = $(".uName").val();
                        var uSex = $(".uSex:checked").val();
                        var birthday = $("#birthday").val();
                        var uPhone = $(".uPhone").val();
                        var uEmail = $(".uEmail").val();
                        var code  = $(".uCode").val();

                        $.ajax({
                            url:"/register",
                            type:"post",
                            data:{uAccount:uAccount,uPassword:uPassword,uName:uName,uSex:uSex,uPhone:uPhone,uBirthday:birthday,uEmail:uEmail,code:code},
                            success:function (result) {
                                if (result.code == 200){
                                    //注册成功
                                    layer.msg(result.msg);
                                    setTimeout('window.location.href="/toLogin"', 1500);
                                } else {
                                    layer.msg(result.msg);
                                }
                            },
                            error:function (result) {
                                layer.msg(result.msg);
                            }
                        });
                        return false;
                    });
                }
            })
        });

        //验证输入的密码是否相同；
        $("#pass2").blur(function() {
            var pass1 = $("#pass1").val();
            var pass2 = $("#pass2").val();
            if(pass1 != pass2) {
                layer.msg("两次输入的密码不一致", {
                    "icon": 2,
                    "time": 2000
                });
                $("#pass2").val("");
                return false;
            }
            return true;
        });

        /* 发送邮箱验证码 */
        $(".codeBtn").click(function () {
            if($(".uEmail").val() == ""){
                $(".codeBtn").attr("disabled","disabled");
                layer.msg('邮箱地址未填');
                setTimeout(function(){
                    $(".codeBtn").removeAttr("disabled");
                },900);
                return;
            }
            var mail = $(".uEmail").val();
            $.ajax({
                url:"/sendIdCode/" + mail,
                type:"get",
                //发送数据的第一种格式，字符串...
                data:'',
                timeout:5000,
                success:function(result){
                    layer.msg(result.msg);
                },
                error:function(result){
                    layer.msg(result.msg);
                }
            });
        });
    });

});